//
//  AppTools.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/6.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "AppTools.h"
#import "AppDelegate.h"
#import "PCCircleViewConst.h"
@implementation AppTools

+(AppTools *)shareInstance
{
    static AppTools *singleAppTools = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (singleAppTools == nil) {
            singleAppTools = [[AppTools alloc] init];
        }
    });
    return (AppTools *)singleAppTools;
}

+ (ETAccountState)checkLoginResponseAllState
{
    UserInfo *userInfo = [AppTools shareInstance].userInfo;
    if ([userInfo.status integerValue] < 60) {
        return ETNOAutonym;
    }else if ([userInfo.status integerValue] == 60){
        return ETBasicAuthentication;
    }else if ([userInfo.status integerValue] == 70)
    {
        return ETAdvancedAuthentication;
    }
    
    //未登录 跳转登录
    return ETNOAutonym;
}

+(BOOL)isIsOpenedGesture
{
    NSString *str = [PCCircleViewConst getGestureWithKey:gestureFinalSaveKey];
    if (str.length > 0) {
        return YES;
    }else
    {
        return NO;
    }
    
}

+(BOOL)isOpenedFingerprint
{
    NSString *str = [PCCircleViewConst getGestureWithKey:fingerprintStatueKey];
    if ([str isEqualToString:@"1"]) {
        return YES;
    }else
    {
        return NO;
    }
}

+(BOOL)validateMobile:(NSString *)mobile{
    /*
    NSString *phoneRegex = @"^134[0-8]\\d{7}$|^13[^4]\\d{8}$|^14[5-9]\\d{8}$|^15[^4]\\d{8}$|^16[6]\\d{8}$|^17[0-8]\\d{8}$|^18[\\d]{9}$|^19[8,9]\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
     */
    if (mobile.length == 11) {
        return YES;
    }else
    {
        return NO;
    }
}

+(BOOL)checkPassword:(NSString *)password
{
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
}

+(void)saveUserAndPwd:(NSString *)userName Pwd:(NSString *)password
{
    [[NSUserDefaults standardUserDefaults] setValue:userName forKey:@"userName"];
    [[NSUserDefaults standardUserDefaults] setValue:password forKey:@"userPwd"];
}

+(NSDictionary *)getUserNameAndPwd
{
    NSString *userName = [[NSUserDefaults standardUserDefaults] valueForKey:@"userName"];
    NSString *userPwd = [[NSUserDefaults standardUserDefaults] valueForKey:@"userPwd"];
    NSDictionary *dic;
    if (userName.length != 0 && userPwd.length != 0) {
        dic = [[NSDictionary alloc]initWithObjectsAndKeys:userName,@"userName",userPwd,@"userPwd", nil];
        
    }
    return dic;
}

+(void)saveLoginStatus:(BOOL )LoginStatus
{
    [[NSUserDefaults standardUserDefaults]setBool:LoginStatus forKey:@"loginStatus"];
}

+(BOOL)isLogin
{
   return [[NSUserDefaults standardUserDefaults] boolForKey:@"loginStatus"];
}

+(void)setGesterWrongTimes:(NSInteger)wrongTimes
{
    [[NSUserDefaults standardUserDefaults] setInteger:wrongTimes forKey:@"gesterWrongTimes"];
}
+(NSInteger)getGesterWrongTimes
{
   return [[NSUserDefaults standardUserDefaults] integerForKey:@"gesterWrongTimes"];
}

+(void)setToken:(NSString *)authToken
{
    [[NSUserDefaults standardUserDefaults] setObject:authToken forKey:@"authToken"];
}
+(NSString *)getauthToken
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"authToken"];
}

+(void)setPhoneNum:(NSString *)phoneNum
{
    [[NSUserDefaults standardUserDefaults] setObject:phoneNum forKey:@"phoneNum"];
}
+(NSString *)getPhoneNum
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNum"];
}

+(void)setFinancialServiceData:(NSData *)data
{
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"FinancialService"];
}
+(NSData *)getFinancialServiceData
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"FinancialService"];
}


//银行卡号截取后4位
+(NSString *)changeCardNum:(NSString *)cardStr
{
    if (cardStr.length > 4) {
        NSString *string = [cardStr substringWithRange:NSMakeRange(cardStr.length - 4, 4)];
        return string;
    }else
    {
        return cardStr;
    }
}



@end
